package com.slicktechnologies.client.views.customer;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.google.gwt.core.shared.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.InlineLabel;
import com.simplesoftwares.client.library.appskeleton.AppMemory;
import com.simplesoftwares.client.library.appstructure.SearchPopUpScreen;
import com.simplesoftwares.client.library.appstructure.SuperTable;
import com.simplesoftwares.client.library.appstructure.formscreen.FormScreen;
import com.simplesoftwares.client.library.appstructure.formscreen.FormScreenPresenter;
import com.simplesoftwares.client.library.appstructure.search.MyQuerry;
import com.simplesoftwares.rebind.annatonations.EntityNameAnnotation;
import com.slicktechnologies.client.services.CsvService;
import com.slicktechnologies.client.services.CsvServiceAsync;
import com.slicktechnologies.client.utility.GeneratedVariableRefrence;
import com.slicktechnologies.client.utility.Screen;
import com.slicktechnologies.client.views.contract.ContractForm;
import com.slicktechnologies.client.views.contract.ContractPresenter;
import com.slicktechnologies.shared.Contract;
import com.slicktechnologies.shared.common.businessprocesslayer.Customer;
import com.slicktechnologies.shared.common.helperlayer.PersonInfo;
/**
 * FormScreen presenter template.
 */
public class CustomerPresenter extends FormScreenPresenter<Customer>{

	//Token to set the concrete FormScreen class name
	CustomerForm form;
	
	public CustomerPresenter  (FormScreen<Customer> view, Customer model) {
		super(view, model);
		form=(CustomerForm) view;
		form.setPresenter(this);
	}

	@Override
	public void reactToProcessBarEvents(ClickEvent e) {
		InlineLabel label=(InlineLabel) e.getSource();
		String text=label.getText().trim();
		
		
		
		if(text.equals("Create Service Card"))
			reactToCreateContract();
		
		if(text.equals("New"))
			reactToNew();

	}

	@Override
	public void reactOnPrint() {


	}

	

	

	@Override
	public void reactOnEmail() {


	}

	/**
	 * Method token to make new model
	 */
	@Override
	protected void makeNewModel() {
		model=new Customer();
	}




	public static void initalize()
	{
		CustomerForm  form=new  CustomerForm();


		CustomerPresenterTable gentable=new CustomerPresenterTableProxy();
		TextColumn<Customer>cust=(TextColumn<Customer>) gentable.getVarRef("getCountColumn");
		gentable.getTable().setColumnWidth(cust,50,Unit.PX);
		
		TextColumn<Customer>salesPerson=(TextColumn<Customer>) gentable.getTable().getColumn(3);
		gentable.getTable().setColumnWidth(salesPerson,150.0,Unit.PX);
		
		TextColumn<Customer>phone=(TextColumn<Customer>) gentable.getTable().getColumn(1);
		gentable.getTable().setColumnWidth(phone,150.0,Unit.PX);
		
		TextColumn<Customer>fullName=(TextColumn<Customer>) gentable.getTable().getColumn(2);
		gentable.getTable().setColumnWidth(fullName,150.0,Unit.PX);
		
		Column<Customer,Date>dateColumn=(Column<Customer, Date>) gentable.getTable().getColumn(4);
		gentable.getTable().setColumnWidth(dateColumn,150.0,Unit.PX);
		
		gentable.setView(form);
		gentable.applySelectionModle();
		CustomerPresenterSearch.staticSuperTable=gentable;
		CustomerPresenterSearch searchpopup=new CustomerPresenterSearchProxy();
		form.setSearchpopupscreen(searchpopup);


		CustomerPresenter  presenter=new CustomerPresenter(form,new Customer());
		AppMemory.getAppMemory().stickPnel(form);
	}

	@EntityNameAnnotation(EntityName = "com.slicktechnologies.shared.common.businessprocesslayer.Customer")
	public static  class CustomerPresenterSearch extends SearchPopUpScreen<Customer>{

		@Override
		public MyQuerry getQuerry() {
			// TODO Auto-generated method stub
			return null;
		}};

		@EntityNameAnnotation(EntityName = "com.slicktechnologies.shared.common.businessprocesslayer.Customer")
		public static class CustomerPresenterTable extends SuperTable<Customer> implements GeneratedVariableRefrence{

			@Override
			public Object getVarRef(String varName) {
				// TODO Auto-generated method stub
				return null;
			}

			@Override
			public void createTable() {
				// TODO Auto-generated method stub

			}

			@Override
			protected void initializekeyprovider() {
				// TODO Auto-generated method stub

			}

			@Override
			public void addFieldUpdater() {
				// TODO Auto-generated method stub

			}

			@Override
			public void setEnable(boolean state) {
				// TODO Auto-generated method stub

			}

			@Override
			public void applyStyle() {
				// TODO Auto-generated method stub

			}} ;

		
			
			private void reactToCreateContract()
			{
				AppMemory.getAppMemory().setCurrentScreenandChangeProcessName("Sales/Contract",Screen.CONTRACT);
				final ContractForm form=ContractPresenter.initalize();
				form.setToNewState();
				final Contract quot=new Contract();
				quot.setBranch(model.getBranch());
				quot.setEmployee(model.getEmployee());
				form.showWaitSymbol();
				PersonInfo personInfo=new PersonInfo();
				personInfo.setCellNumber(model.getCellNumber1());
				personInfo.setCount(model.getCount());
				if(model.isCompany()==true)
					   personInfo.setFullName(model.getCompanyName());
					else
					{
						model.fillAllNames();
						personInfo.setFullName(model.getFullname());
						 
					}
				quot.setCinfo(personInfo);
				quot.setStatus(Contract.CREATED);
				Timer t = new Timer() {
				      @Override
				      public void run() {
				    	 
				    	  form.hideWaitSymbol();
				    	  form.updateView(quot);
				    	 
				    	  form.getPersonInfoComposite().setEnabled(false);
				    	  
				    	  
				        
				      }
				    };
				    t.schedule(5000);
			}
			
			private void reactToNew()
			{
				form.setToNewState();
				this.initalize();
				form.toggleAppHeaderBarMenu();
			}

			
			@Override
			public void reactOnDownload() {
				
				
					ArrayList<Customer> custarray=new ArrayList<Customer>();
					List<Customer> list=(List<Customer>) form.getSearchpopupscreen().getSupertable().getDataprovider().getList();
					
					custarray.addAll(list);
					CsvServiceAsync csvservice=GWT.create(CsvService.class);
					
					csvservice.setcustomerlist(custarray, new AsyncCallback<Void>() {

						@Override
						public void onFailure(Throwable caught) {
							System.out.println("RPC call Failed"+caught);
						}
						
						@Override
						public void onSuccess(Void result) {
				
							String gwt=com.google.gwt.core.client.GWT.getModuleBaseURL();
							final String url=gwt + "csvservlet"+"?type="+1;
							Window.open(url, "test", "enabled");
						}
					});
			}

}
